package iscte.ai.kcgtd;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;

public class SeeTasks extends Activity{
	private ArrayList<Task> tarefas;
	private String tarefasStr;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		tarefasStr = (String) getIntent().getExtras().get("iscte.ai.kcgtd.tarefas_para_mostrar");
		ListView list = (ListView) findViewById(R.id.list);
		
		switch (tarefasStr) {
			case MainActivity.CATEGORIA_PROJECTOS:
				tarefas = MainActivity.projectos;
				break;
			case MainActivity.CATEGORIA_LAZER:
				tarefas = MainActivity.lazer;
				break;
			case MainActivity.CATEGORIA_COMPRAS:
				tarefas = MainActivity.compras;
			break;
	
			default:
				break;
		}

		TaskViewAdapter adapter = new TaskViewAdapter(this, R.layout.task_item, tarefas);
		list.setAdapter(adapter);
		
		if(!tarefas.isEmpty()){
			// Register the ListView  for Context menu
	        registerForContextMenu(list);
		}
		
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Opções");  
        menu.add(0, v.getId(), 0, "Ver Objectivo");  
        menu.add(0, v.getId(), 0, "Apagar Objectivo"); 
	    
	}
	
	@Override  
	public boolean onContextItemSelected(MenuItem item){  
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();                
	    //  info.position will give the index of selected item
	    int IndexSelected = info.position;
	    
	    if(item.getTitle()=="Ver Objectivo"){             
	        // Code to execute when clicked on This Item
	    	Toast.makeText(getApplicationContext(), tarefas.get(IndexSelected).getName(), Toast.LENGTH_SHORT).show();
	    }else if(item.getTitle()=="Apagar Objectivo"){
	        // Code to execute when clicked on This Item
	    	Task t = tarefas.get(IndexSelected);
	    	tarefas.remove(t);
	    	Toast.makeText(getApplicationContext(), "Objectivo " + t.getName() + " apagado!", Toast.LENGTH_SHORT).show();
	    	//GRAVAR AS LISTAS
			Bag bag = new Bag();
			bag.fillBag();
			Utils.saveData(this, bag);
	    	//Fazer refresh à actividade
			finish();
	        startActivity(getIntent());
	    }else{
			return false;
	    }  
	    
	    return true;  
	}
	
}
